package com.five.taotao.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.five.taotao.R;
import com.five.taotao.entity.Product;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by MQ on 2016/11/4.
 */
public class GoodsCollectAdapter extends RecyclerView.Adapter<GoodsCollectAdapter.myViewHolder> {
    @BindView(R.id.checked)
    ImageView checked;
    //定义两个变量，一个context一个集合
    private Context context;
    private List<Product.ListBean> mDatas;
    private int flag=1;
    /**
     * 有参构造
     *
     * @param context 传入context
     * @param mDatas  传入数据
     */
    public GoodsCollectAdapter(Context context, List<Product.ListBean> mDatas) {
        super();
        this.context = context;
        this.mDatas = mDatas;

    }

    /**
     * 关联每个item布局
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        myViewHolder viewHolder = new myViewHolder(
                LayoutInflater.from(context)
                        .inflate(R.layout.goods_collect, parent, false));


        return viewHolder;
    }
/*   private  void  isShowChecked(){
       View view= (View) context.getResources().getLayout(R.layout.activity_product_details);
       TextView textView= (TextView) view.findViewById(R.id.edit);
       String str=textView.getText().toString();
       if(str.equals("完成")){
           checked.setVisibility(View.VISIBLE);
           flag=1;
       }else{
           checked.setVisibility(View.GONE);
           flag=0;
       }
   }*/
    @OnClick(R.id.checked)
    public void onClick() {

        if(flag==0){
            checked.setVisibility(View.VISIBLE);
            flag=1;
        }else{
            checked.setVisibility(View.GONE);
            flag=0;
        }
    }


    /**
     * 将传进来的数据设置到viewholder之中
     * 实现数据的传入
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(myViewHolder holder, int position) {
        holder.name.setText(mDatas.get(position).getName());
        holder.price.setText("¥" + mDatas.get(position).getPrice());
        holder.vipprice.setText("会员价：￥" + mDatas.get(position).getVipprice());
        holder.introduce.setText(mDatas.get(position).getIntroduce());

       /* Picasso.with(context)
                .load(mDatas.get(position).getUrl())
                .placeholder(R.drawable.chat_image_normal)
                .error(R.drawable.chat_image_pressed)
                .centerCrop()
                .into(holder.img);*/
        Glide.with(context)
                .load(mDatas.get(position).getUrl())
                .crossFade()
                .centerCrop()
                .into(holder.img);

    }

    /**
     * 返回item的数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 自定义的一个内部类继承RecyclerView.ViewHolder
     * 定义item中的所有变量并与布局之中关联
     */
    class myViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView name;
        TextView introduce;
        TextView price;
        TextView vipprice;

        public myViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
            name = (TextView) itemView.findViewById(R.id.name);
            introduce = (TextView) itemView.findViewById(R.id.introduce);
            price = (TextView) itemView.findViewById(R.id.price);
            vipprice = (TextView) itemView.findViewById(R.id.vipprice);

        }
    }


}


